<!DOCTYPE html>
<html lang="zh_CN">
<head>
    <#include "../common/header.ftl">
</head>
<body>

<div class="container" style="margin-top: 20px">
    <#include "../common/top.ftl">
    <div class="row">
        <div class="col-sm-3">
            <#include "../common/menu.ftl">
        </div>
        <div class="col-sm-9">
            <div class="row">
                <div class="col-sm-12">
                    <h1 class="page-head-line">日报管理</h1>
                </div>
            </div>
            <#--高级查询--->
            <form class="form-inline" id="searchForm" action="/daily/list.do" method="post">
                <input type="hidden" name="currentPage" id="currentPage" value="1">
                <div class="form-group">
                    <label>关键字:</label>
                    <input type="text" class="form-control" name="keyword" value="${(qo.keyword)!}"
                           placeholder="请输入标题/副标题">
                </div>
                <button id="btn_query" class="btn btn-default">
                    <span class="glyphicon glyphicon-search"></span> 查询
                </button>
                <a href="javascript:" class="btn btn-success inputBtn">
                    <span class="glyphicon glyphicon-plus"></span> 添加日报
                </a>
            </form>

            <table class="table table-striped table-hover" >
                <tr>
                    <th>编号</th>
                    <th>标题</th>
                    <th>副标题</th>
                    <th>封面</th>
                    <th>创建时间</th>
                    <th>状态</th>
                    <th></th>
                </tr>
                <#--使用Freemark的指令完成循环迭代-->
                <#list pageInfo.list as entity>
                    <tr>
                        <td>${entity_index + 1}</td>
                        <td>${entity.title!}</td>
                        <td>${entity.subTitle!}</td>
                        <td><img src="${entity.coverUrl}" width="30px" /></td>
                        <td>${(entity.createTime?string('yyyy-MM-dd HH:mm:ss'))!}</td>
                        <td>
                                <#if entity.state==1 >
                                    启用
                                    <#elseif entity.state==0>
                                    禁用
                                </#if>
                        </td>
                        <td>
                            <a href="javascript:" class="btn btn-info btn-xs showBtn" data-tid="${entity.id}">
                                <span class="glyphicon glyphicon-search"></span> 查看
                            </a>
                            <a href="javascript:" class="btn btn-success btn-xs inputBtn" data-json='${entity.json}'>
                                <span class="glyphicon glyphicon-pencil"></span> 修改
                            </a>
                            <a href="javascript:" class="btn btn-danger  btn-xs deleteBtn" data-tid="${entity.id}">
                                <span class="glyphicon glyphicon-remove"></span> 删除
                            </a>
                        </td>
                    </tr>
                </#list>
            </table>

            <#include "../common/page.ftl">
        </div>
    </div>
</div>


<#--模态框必须是body的子元素-->
<div class="modal fade" id="inputModal" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
        <#--模态框标题-->
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">查看内容</h4>
            </div>
        <#--模态框正文-->
            <div class="modal-body">

            </div>
        </div>
    </div>
</div>

<#--模态框必须是body的子元素-->
<div class="modal fade" id="dailyModal" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog ">
        <div class="modal-content">
        <#--模态框标题-->
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title">新增/编辑</h4>
            </div>
        <#--模态框正文-->
            <div class="modal-body1">
            <#--填充编辑界面-->
                <form class="form-horizontal" action="/daily/saveOrUpdate.do" method="post" id="dailyForm"
                      enctype="multipart/form-data">
                    <input type="hidden" name="id"/>
                    <div class="form-group">
                        <label class="col-lg-4 control-label">标题：</label>
                        <div class="col-lg-6">
                            <input type="text" class="form-control" name="title" placeholder="请输入日报标题"/>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-4 control-label">副标题：</label>
                        <div class="col-lg-6">
                            <input type="text" class="form-control" name="subTitle" placeholder="请输入日报副标题"/>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-4 control-label">封面：</label>
                        <div class="col-lg-6">
                            <img id="coverImg" width="100%"> <!--用来显示-->
                            <input type="hidden" class="form-control" name="coverUrl"/><!--用来提交表单(回显)-->
                            <input type="file" class="form-control" name="file"/><!--用来提交表单(上传)-->
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-lg-4 control-label">状态：</label>
                        <div class="col-lg-6">
                            <select name="state" class="form-control">
                                <option value="0">禁用</option>
                                <option value="1">启用</option>
                            </select>
                        </div>
                    </div>
                    //文本编辑框
                    <textarea name="dailyContent.content" id="editor1"></textarea>
                </form>

            </div>
        <#--模态框底部按钮-->
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary dailyBtn">保存</button>
            </div>
        </div>
    </div>
</div>
<!--引入ckeditor-->
<script src="/js/plugins/ckeditor/ckeditor.js"></script>

<script>
    $(function () {
        //查看文章
        $(".showBtn").click(function () {
            //获取文章内容
            var id = $(this).data("tid");

            $.get('/daily/getContentById.do',{id:id},function (data) {
                $(".modal-body").html(data.content);
                $("#inputModal").modal('show');
            });
        });

        //初始化ckeditor
        var ck = CKEDITOR.replace('editor1');

        //模态框的表单提交
        $(".dailyBtn").click(function () {
            //获取编辑器中的内容,设置到textarea里面
            var data = ck.getData();
            $("#editor1").html(data);
            //提交表单
            $("#dailyForm").submit();
        });
        //删除日报文章
        $(".deleteBtn").click(function () {
            var id = $(this).data("tid");

            $.get('/daily/delete.do',{id:id},function (data) {
                location.reload();
            });
        });

        //触发按键，弹出模态框
        $(".inputBtn").click(function () {
            //清空模态框的数据
            $("#dailyModal input").val("");
            $("#coverImg").attr('src', '');
            //清空文本编辑框内容
            // ck.setData("");
            //获取当前按钮中的自定义JSON对象
            var obj = $(this).data("json");
            //只有编辑按钮才做数据回显
            if (obj) {
                $("#dailyModal input[name='id']").val(obj.id);
                $("#dailyModal input[name='title']").val(obj.title);
                $("#dailyModal input[name='subTitle']").val(obj.subTitle);
                $("#dailyModal select[name='state']").val(obj.state);
                $("#dailyModal input[name='coverUrl']").val(obj.coverUrl);
                $("#coverImg").attr('src', obj.coverUrl);
                //回显攻略文章内容
                $.get('/daily/getContentById.do', {id: obj.id}, function (data) {
                    ck.setData(data.content);
                });
            }
            $("#dailyModal").modal('show');
        });
    })
</script>
<style>
    #inputModal img{
        width:100%
    }

    #inputModal .modal-body{
        max-height: 600px;
        overflow-y: auto;
    }
</style>
</body>
</html>